﻿#include "redis_query_result.h"
#include "hiredis/hiredis.h"


CRedisQueryResult::CRedisQueryResult( redisReply * pRedisR )
	              : m_pReply(pRedisR)
	              , m_cur_element(0)
{

}

void CRedisQueryResult::FreeReply()
{
	if (m_pReply != nullptr)
	{
		freeReplyObject(m_pReply);
	}
}

char* CRedisQueryResult::GetString()
{
	if (m_pReply != nullptr)
	{
		if (m_pReply->type== REDIS_REPLY_STRING || m_pReply->type == REDIS_REPLY_STATUS )
		{
			return m_pReply->str;
		}	
	}

	return 0;
}

int CRedisQueryResult::GetReplyType()
{

	if (m_pReply != nullptr)
	{
		return m_pReply->type;
	}

	return -1;
}

int64_t CRedisQueryResult::GetInt64()
{
	if (m_pReply)
	{
		if (m_pReply->type == REDIS_REPLY_INTEGER )
		{
			return m_pReply->integer;
		}	
	}
	return -1;
}


int CRedisQueryResult::GetArrayLenth()
{
	if(m_pReply)
	{
		if (m_pReply->type == REPLY_ARRAY )
		{
			return m_pReply->elements;
		}	
	}

	return 0;
}

redisReply* CRedisQueryResult::GetNextElement()
{
	if (m_pReply)
	{
		if(m_pReply->type == REPLY_ARRAY &&  m_cur_element < m_pReply->elements )
		{
			return m_pReply->element[m_cur_element++];
		}
	}

	return 0;
}

